﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using ARMS.Models;
using ARMS.BaseClasses;

namespace ARMS.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/
        private int shopid = 1;
        private const string CSHOP = "Shop"; // this is done due to not having the user package implemented 
        // the logged in user user.shopid will rather be used instead.
        // to be done for in next phase.

        public ActionResult Index()
        {
            try
            {
                CustomerModel model = new CustomerModel();
                DataTable customers = model.getCustomers(this.shopid);

                if (customers.Rows.Count < 1)
                {
                    throw new Exception("No customers found for this shop!");
                }

                ShopModel modelShop = new ShopModel();
                Shop shop = modelShop.GetShop(this.shopid);

                ViewData[CSHOP] = shop;

                return View(customers);
            }
            catch (Exception e)
            {
                HandleErrorInfo err = new HandleErrorInfo(e, "Main", "Index");
                return View("Error", err);
            }
        }
        public ActionResult CreateCustomer()
        {
            try
            {
                ShopModel modelShop = new ShopModel();
                Shop shop = modelShop.GetShop(this.shopid);
                Customer customer = new Customer();
                customer.id = -1;
                customer.shopid = this.shopid;

                ViewData[CSHOP] = shop;
                return View(customer);
            }
            catch (Exception e)
            {
                HandleErrorInfo err = new HandleErrorInfo(e, "Main", "Index");
                return View("Error", err);
            }
        }
        public ActionResult Search()
        {
            try
            {
                ShopModel modelShop = new ShopModel();
                Shop shop = modelShop.GetShop(this.shopid);
                Customer customer = new Customer();
                customer.shopid = this.shopid;

                ViewData[CSHOP] = shop;
                return View(customer);
            }
            catch (Exception e)
            {
                HandleErrorInfo err = new HandleErrorInfo(e, "Main", "Index");
                return View("Error", err);
            }
        }
        public ActionResult Save(Customer customer)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ShopModel modelShop = new ShopModel();
                    Shop shop = modelShop.GetShop(this.shopid);

                    ViewData[CSHOP] = shop;
                    return View("Detail", customer);
                }

                CustomerModel model = new CustomerModel();
                customer.shopid = this.shopid;

                if (customer.id < 1) {
                    model.createCustomer(customer);
                }else{
                    model.updateCustomer(customer);
                }

                return RedirectToAction("Index", "Customer");
            }
            catch (Exception e)
            {
                HandleErrorInfo err = new HandleErrorInfo(e, "Customer", "Index");
                return View("Error", err);
            }
        }
        [HttpPost]
        public ActionResult DeleteCustomer(int id)
        {
            try
            {
                CustomerModel model = new CustomerModel();
                model.deleteCustomer(id);

                return View("DeleteCustomer");
            }
            catch (Exception e)
            {
                return View("Error", e);
            }
        }
        public ActionResult Detail(int id)
        {
            try
            {
                CustomerModel model = new CustomerModel();
                Customer customer = customer = model.getCustomer(id);


                ShopModel modelShop = new ShopModel();
                Shop shop = new Shop();
                shop = modelShop.GetShop(this.shopid);

                ViewData[CSHOP] = shop;
                return View("Detail", customer);
            }
            catch (Exception e)
            {
                HandleErrorInfo err = new HandleErrorInfo(e, "Customer", "Index");
                return View("Error", err);
            }
        }
    }
}
